<?php

namespace app\admin\controller;

use app\common\model\Cate as CateModel;
use app\common\model\Article as ArticleModel;
use app\common\validate\CateValidate;
use think\Cache;

class Cate extends AdminCommon
{

    /**
     * 分类列表
     * @desc 分类列表
     */
    public function index()
    {
        $params = $this->request->param();
        $where = [];
        if (!empty($params['search_field']) && !empty($params['search_value'])) {
            $where[$params['search_field']] = $params['search_value'];
        }
        $cateModel = new CateModel();
        $list = $cateModel->alias('a')->join('cate b', 'a.pid=b.id', 'LEFT')->field('a.*,b.cate_name as p_name')->where($where)->order(['a.id' => 'desc'])->paginate();
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加分类（目前最多支持二级分类）
     * @desc 添加分类（目前最多支持二级分类）
     */
    public function add()
    {
        $params = $this->request->param();
        if ($this->request->isAjax() && $this->request->isPost() && isset($params['req_sub'])) {
            //校验器
            $cateValidate = new CateValidate();
            if (!$cateValidate->scene('add')->check($params)) {
                $this->error($cateValidate->getError());
            }
            $cateModel = new CateModel($params);
            if ($cateModel->allowField(true)->save() !== false) {
                //清除缓存
                Cache::rm('cate_choices_list');
                Cache::rm('cate_home_choices_list');
                $this->success('添加成功', 'admin/cate/index');
            } else {
                $this->error('添加失败');
            }
        } else {
            $cateModel = new CateModel();
            $this->assign('parentCateList', $cateModel->getParentCateList());
            return $this->fetch();
        }
    }

    /**
     * 编辑分类
     * @desc 编辑分类
     */
    public function edit($id)
    {
        $params = $this->request->param();
        if ($this->request->isAjax() && $this->request->isPost() && isset($params['req_sub'])) {
            //校验器
            $cateValidate = new CateValidate();
            if (!$cateValidate->scene('add')->check($params)) {
                $this->error($cateValidate->getError());
            }
            $cateModel = new CateModel();

            if ($cateModel->allowField(true)->save($params, ['id' => $id]) !== false) {
                //清除缓存
                Cache::rm('cate_choices_list');
                Cache::rm('cate_home_choices_list');
                $this->success('编辑成功', 'admin/cate/index');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $cateModel = new CateModel();
            $info = $cateModel->get($params['id']);
            $this->assign('info', $info);
            $this->assign('parentCateList', $cateModel->getParentCateList());
            return $this->fetch();
        }
    }

    /**
     * 删除分类
     * @desc 删除分类
     */
    public function del($id)
    {
        if ($this->request->isAjax()) {
            $cateModel = new CateModel();
            if ($cateModel->where(['pid' => $id])->find()) {
                $this->error('删除失败,存在下级分类');
            }
            $articleModel = new ArticleModel();
            if($articleModel->where(['cate_id'=>$id])->find()){
                $this->error('删除失败,存在分类文章');
            }
            if ($cateModel->where(['id' => $id])->delete() === false) {
                $this->error('删除失败');
            } else {
                //清除缓存
                Cache::rm('cate_choices_list');
                Cache::rm('cate_home_choices_list');
                $this->success('删除成功', 'admin/cate/index');
            }
        }
    }
}